package com.ywl.mvcframework.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import com.ywl.demo.form.LoginActionForm;
import com.ywl.mvcframework.actionform.ActionForm;

/**
 * 从request里面取参数封装成ActionForm
 * @author wl.yang
 *
 */
public class RequestParamUtil {

	/**
	 * 从request中封装ActionForm
	 * @param request
	 * @param cla
	 * @return
	 */
	public static ActionForm generateActionForm(HttpServletRequest request, Class cla){
		ActionForm objForm = null;
		try {
			Constructor constructor = cla.getConstructor();
			objForm = (ActionForm)constructor.newInstance();
			
			//遍历
			Enumeration<String> names = request.getParameterNames();
			String param = null;
			String paramVal = null;
			while(names.hasMoreElements()){
				param = names.nextElement();
				paramVal = request.getParameter(param);
				
				//获得需要执行的方法
				RequestParamUtil.setActionFormVal(cla, objForm, param, paramVal);
			}
			
		} catch (NoSuchMethodException | SecurityException
				| InstantiationException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException e) {
			e.printStackTrace();
		}
		
		return objForm;
	}

	/**
	 * 利用反射给对象设置值
	 * @param objForm
	 * @param param
	 * @param paramVal
	 */
	private static void setActionFormVal(Class cla, ActionForm objForm, String param,
			String paramVal) {

		String exeMethod = RequestParamUtil.getExeMethod(param);
		
		Method[] methods = cla.getMethods();
		for(Method method : methods){
			String mName = method.getName();
			if(mName.equals(exeMethod)){
				//处理参数
				try {
					
					Class[] paramTypes = method.getParameterTypes();
					Class paramType = paramTypes[0];
					Object val = RequestParamUtil.getParamTypeVal(paramType, paramVal);
					
					method.invoke(objForm, new Object[]{val});
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
				break;
			}
		}
	}

	/**
	 * 根据参数类型转变参数值类型
	 * @param paramType
	 * @param paramVal
	 * @return
	 */
	private static Object getParamTypeVal(Class paramType, String paramVal) {
		
		String typeName = paramType.getName();
		System.out.println(typeName);
		
		switch(typeName){
		case "int":
			return Integer.parseInt(paramVal);
		case "long":
			return Long.parseLong(paramVal);
		case "double":
			return Double.parseDouble(paramVal);
		case "float":
			return Float.parseFloat(paramVal);
		case "short":
			return new Short(paramVal);
		case "boolean":
			return new Boolean(paramVal);
		case "java.lang.String":
			return paramVal;
		case "java.lang.Integer":
			return Integer.parseInt(paramVal);
		case "java.lang.Long":
			return Long.parseLong(paramVal);
		case "java.lang.Double":
			return Double.parseDouble(paramVal);
		case "java.lang.Float":
			Float.parseFloat(paramVal);
		}
		
		return paramVal;
	}

	/**
	 * 将字段名转成方法
	 * @param param
	 * @return
	 */
	private static String getExeMethod(String param) {
		StringBuffer sb = new StringBuffer();
		sb.append("set");
		String firstChar = (param.charAt(0)+"").toUpperCase();
		sb.append(firstChar);
		sb.append(param.substring(1));
		
		return sb.toString();
	}
}
